package com.offcn.order.service.impl;

import com.offcn.dycommon.enums.OrderStatusEnum;
import com.offcn.dycommon.response.AppResponse;
import com.offcn.order.mapper.TOrderMapper;
import com.offcn.order.pojo.TOrder;
import com.offcn.order.service.OrderService;
import com.offcn.order.service.ProjectServiceFeign;
import com.offcn.order.vo.req.OrderInfoSubmitVo;
import com.offcn.order.vo.resp.TReturn;
import com.offcn.utils.AppDateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/01/27/19:08
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private TOrderMapper orderMapper;

    @Autowired
    private ProjectServiceFeign projectServiceFeign;

    /**
     * 保存订单方法
     *
     * @param vo
     * @return
     */
    @Override
    public TOrder saveOrder(OrderInfoSubmitVo vo) {
        //1.创建订单对象
        TOrder order = new TOrder();
        String accessToken = vo.getAccessToken();
        //得到会员ID
        String memberId = stringRedisTemplate.opsForValue().get(accessToken);
        order.setMemberid(Integer.parseInt(memberId));
        //2.复制对象 vo ->> order
        BeanUtils.copyProperties(vo, order);
        //产生订单编号
        String orderNum = UUID.randomUUID().toString().replace("-", "");
        order.setOrdernum(orderNum);
        order.setStatus(OrderStatusEnum.UNPAY.getCode() + "");//未支付状态
        order.setInvoice(vo.getInvoice().toString());//发票状态
        order.setCreatedate(AppDateUtils.getFormatTime());//创建时间
        //3.远程服务调用 查询回报增量列表
        AppResponse<List<TReturn>> response = projectServiceFeign.getReturnList(vo.getProjectid());
        List<TReturn> returnList = response.getData();
        //默认取得第一个回报增量信息
        TReturn tReturn = returnList.get(0);
        //支持金额 回报数量*回报支持金额+运费
        Integer money = order.getRtncount() * tReturn.getSupportmoney() + tReturn.getFreight();
        order.setMoney(money);
        //4.执行保存操作
        orderMapper.insertSelective(order);

        return order;
    }
}
